Skip to content

Don't report includes that need the container at the top level - #1066

Merged
mglaman merged 2 commits into
mainfrom
load-include-container-not-initialized
Nov 5, 2026
Merged

mglaman merged 2 commits into
mainfrom
load-include-container-not-initialized

Conversation

@mglaman

@mglaman mglaman commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Fixes #1062.

LoadIncludes and ModuleLoadInclude run require_once on the include during analysis. If the file's top-level code uses the container, it throws ContainerNotInitializedException, and the rules report A file could not be loaded.

Core hits this with entity_test.install:

$module_handler = \Drupal::moduleHandler();

The file exists and its functions are defined, so the error is a false positive. It also looks random. require_once marks the file as included even when it throws, so only the first call in each process fails. I expect that's why the failures move around between core's runs: the first call depends on how files are split across PHPStan workers.

Catch the exception and treat the include as loaded. DrupalAutoloader::loadAndCatchErrors() already handles this exception, and its .install loading skips entity_test outright. Other exceptions still report an error.

This keeps require_once instead of only checking that the file exists, because analysis needs the functions the include declares. entity_test_install() is called right after loadInclude() in core's tests.

Testing

  • Added a test for each rule, using includes that call \Drupal::moduleHandler() at the top level. Both failed before the fix.
  • Ran two loadInclude('entity_test', 'install') calls against the real core file in one process. Before the fix, only the first reports an error. After the fix, neither does.

Lint is red until #1064 and #1065 land. Neither error comes from this change.

🤖 Generated with Claude Code

mglaman and others added 2 commits November 5, 2026 09:15
loadInclude() and module_load_include() run require_once during analysis.
When the file's top-level code calls \Drupal::something(), it throws
ContainerNotInitializedException, which was reported as an unloadable file.
PHP won't re-run the file, so only the first call in each process failed.
That's why core saw these errors at random.

Treat the exception as loaded, matching DrupalAutoloader::loadAndCatchErrors().

Fixes #1062

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mglaman
mglaman merged commit a6990c2 into main Nov 5, 2026
19 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Core analysis fails randomly with loadIncludes.fileNotLoadable errors

1 participant